package com.mango.community.util;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

/**
 * @Author:  MangoCookieMilk
 * @Date:     2020/7/25 16:15
 *从request 中获取cookie的方法
 */
public class CookieUtil {

    /**
     * @param request
     * @param name
     * @return
     * 从request中获取cookie，同过传来的name（cookie的key）
     */
    public static String getValue(HttpServletRequest request,String name){
        //判断空值，为空抛出一个异常
        if (request == null || name == null){
            throw  new IllegalArgumentException("参数不能为空");
        }
        Cookie[] cookies = request.getCookies();
        if(cookies !=null){
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals(name)){
                    return cookie.getValue();
                }
            }
        }
        return null;
    }
}
